package com.csc.contact.checkbill.utiltools.ftputils;

import com.csc.contact.checkbill.entity.ReconciliationInfo;
import com.csc.contact.checkbill.service.ReconciliationServiceImpl;
import com.csc.contact.checkbill.controller.ReadFileFromFTPTask;
import org.apache.log4j.Logger;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class FtpReadFileImpl implements FtpReadFile {
    private static Logger logger = Logger.getLogger(FtpReadFileImpl.class);
    public static int dataNum = 0;//提交数据的条数
    ReconciliationInfo reconciliationInfo = null;
    public static final String DECOLLATO = ",";  //分割符


    private FtpReadFileImpl() {

    }

    private static FtpReadFileImpl ftpReadFile = null;

    public static FtpReadFileImpl getFtpReadFileIns() {
        if (ftpReadFile == null) {
            return new FtpReadFileImpl();
        }
        return ftpReadFile;
    }

    @Override
    public void readFile(String time, String date, String line, int num) {

    }

    @Override
    public void readFile(String name, String line, int num, List<ReconciliationInfo> reconciliationInfos, ReconciliationServiceImpl
            reconciliationService ,List<String> queryY ,String proY) {
        logger.info("进入往对象里面装数据=======对象列表大小====" + reconciliationInfos.size());
        try {
            FtpReadFileImpl.dataNum++;
            String[] datas = line.split(DECOLLATO, -1);
            logger.info("分割后的数组大小======" + datas.length);
            boolean flag = false;
            //发起地业务流水号、缴费金额、被缴费号码、缴费卡号不为空
            if (datas.length == 18 && !datas[3].isEmpty() && !datas[4].isEmpty() && !datas[7].isEmpty() && !datas[8].isEmpty() && num != 0) {
                //如果当前文件为各省落地方组合缴费文件
                if(!"93".equals(proY)) {
                    //如果 卡归属省编码属于已割接 且 卡号同省 ABB
                    if (queryY.contains(datas[10]) && datas[14].equals(datas[10])) {
                        flag = true;
                    }
                }else{
                    flag = true;
                }
                if(flag){
                    reconciliationInfo = new ReconciliationInfo();
                    logger.info("num====" + num);
                    reconciliationInfo.setTransTime(datas[1]);
                    reconciliationInfo.setDealTime(datas[2]);
                    reconciliationInfo.setOriginSerialNo(datas[3]);
                    reconciliationInfo.setCgiMoney(Integer.parseInt(datas[4]));
                    reconciliationInfo.setOriginProvince(Integer.parseInt(datas[5]));
                    reconciliationInfo.setCgiAccnum(datas[7]);
                    reconciliationInfo.setCgiCardnum(datas[8]);
                    reconciliationInfo.setCgiChannelType(datas[9]);
                    reconciliationInfo.setCgiCardprovince(Integer.parseInt(datas[10]));
                    reconciliationInfo.setCgiCardcity(datas[11]);
                    reconciliationInfo.setCgiAccbasetype(datas[12]);
                    reconciliationInfo.setCgiSystemcode(datas[13]);
                    reconciliationInfo.setCgiAccprovince(datas[14]);
                    reconciliationInfo.setCgiAcccity(datas[15]);
                    reconciliationInfo.setCgiIsgift(datas[16]);
                    reconciliationInfo.setCgiCardnet(datas[17]);
                    logger.info("reconciliationInfo======" + reconciliationInfo.toString());
                    reconciliationInfos.add(reconciliationInfo);
                }
            }
            if (num % ReadFileFromFTPTask.BATCHSIZE == 0) { //每1000条数据提交一次
                reconciliationService.batchReconciliationInfo(reconciliationInfos);
                logger.info(name + "第" + (num / ReadFileFromFTPTask.BATCHSIZE) + "批数据导入：" + num);
                reconciliationInfos.clear();
                FtpReadFileImpl.dataNum = 0;
            }
        } catch (Exception e) {
            logger.info("封装对象数据出错=====", e);
        }

    }


}